#ifndef _CAN_APP_H
#define _CAN_APP_H

#include <linux/can.h>
#include <pthread.h>

#include <iostream>
#include <list>

#define MAX_NUM_FILTER 20


typedef struct _filter_array
{
    int num;
    struct can_filter filters[MAX_NUM_FILTER];
}filter_array; 

#define MAX_COUNT_CAN_FRAME 100


//type 
typedef struct _can_gram
{
	char opcode;
	char param;
	char reserved[6];
}can_gram;

typedef enum
{
	TYPE_UIM = 1,
	TYPE_LEC = 2
}MQ_TYPE;


typedef std::list<struct can_frame> listCanFrame;

#define MASK_RECV_UIM (1<<24)

typedef struct _msg_can_frame
{
    long type;
    struct can_frame frame;
}msg_can_frame;


typedef struct _board_frame
{
	unsigned char boardId;
	unsigned char dataType;
	unsigned char data[6];
}board_frame;

#define MAX_NUM_ELE_ON_ELE_BOARD 4

typedef struct _eleInfoOnBoard
{
    int portNum;
    int currPos;
	int posToMove;
	int max_distance;
	float thetaPerStep;
    float lead;
	unsigned char msgSrc;
}eleInfoOnBoard;

#define INCREMENT_POS 0

#define MAX_NAME_LEN 32

class CCan
{
private:
	//ÍšÑ¶Ïß³Ì±êÊ¶·ûID
	pthread_t 	m_thread_recv;
	pthread_t 	m_thread_proc;
	int m_MQ_keyId;
	int m_MQ_type;
	// Ž®¿ÚÊýŸÝœÓÊÕÏß³Ì
	static int CanReceiveThreadFunc( void* lparam );

	static int CanDataProcThreadFunc(void* lparam);

	char m_portName[MAX_NAME_LEN];
public:
	CCan();
	virtual ~CCan();

	// ÒÑŽò¿ªµÄŽ®¿ÚÎÄŒþÃèÊö·û
	int m_fd;

	// Ž®¿ÚÊýŸÝœÓÊÕBuffer
	listCanFrame m_recvFrameBuff;

	// ÍË³öÊýŸÝœÓÊÕÏß³Ì±êÖŸ
	int  m_ExitThreadFlag;

	// °ŽÕÕÖž¶šµÄŽ®¿Ú²ÎÊýŽò¿ªŽ®¿Ú£¬²¢ŽŽœšŽ®¿ÚœÓÊÕÏß³Ì
	int  OpenCanPort( int portNum, filter_array * filtersIn );
	// ¹Ø±ÕŽ®¿Ú²¢ÊÍ·ÅÏà¹Ø×ÊÔŽ
	int  CloseCanPort( );
	// ÓÃÓÚÏòŽ®¿ÚÐŽÊýŸÝ
	int  WriteCanPort( struct can_frame* frameIn);
        //set filters for the can port
    int setFilters(filter_array * filtersIn);
	// œÓÊÕŽ®¿ÚÊýŸÝŽŠÀíº¯Êý
	virtual int PackagePro( void * frame );

	void setKeyId_FrameMQ(int keyId);

	int getKeyId_FrameMQ(void);

	virtual void cmdAct(void * param);

	int getType_MQ(void);
	void setType_MQ(int type);

	void clearData(void);

	int sendString(unsigned short sid, int len, unsigned char * buff);
};

extern eleInfoOnBoard eleOnBoardArr[MAX_NUM_ELE_ON_ELE_BOARD];

#endif
